﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Runtime.Serialization;
using System.Windows.Navigation;
using Hammock.Web;
using MyMeetup;
using MyMeetup.Entities;

public enum RSVPType
{
    Yes, No
}

[DataContract]
public partial class Events
{
    [DataMember(Name = "results")]
    public List<Event> Items { get; set; }
    [DataMember]
    public object meta { get; set; }

}

public class EventSelf
{
    public EventSelf()
    {
        rsvp = new Rsvp();
        rsvp.response = "none";
    }
    public bool checked_in { get; set; }
    public List<object> actions { get; set; }
    public Rsvp rsvp { get; set; }
}



public partial class Event
{
    public string visibility { get; set; }
    public string status { get; set; }
    public EventSelf self { get; set; }
    public Venue venue { get; set; }
    public int utc_offset { get; set; }
    public string id { get; set; }
    public long time { get; set; }
    public int? rsvp_limit { get; set; }
    public int yes_rsvp_count { get; set; }
    public int no_rsvp_count { get; set; }
    public int maybe_rsvp_count { get; set; }
    public string event_url { get; set; }
    public string how_to_find_us { get; set; }
    public string description { get; set; }
    public string name { get; set; }
    public Group group { get; set; }
    public string photo_url { get; set; }
    public RsvpRules rsvp_rules { get; set; }

    public string organizer_id {get; set;}
    //public string guest_limit {get; set;}
    //public string utc_rsvp_open_time {get; set;}
    //public string photo_count {get; set;}
    //public string rsvpable {get; set;}
    public string myrsvp {get; set;}
    //public string maybe_rsvpcount {get; set;}
    //public string waiting_rsvpcount {get; set;}
    //public string my_waitlist_status {get; set;}
    //public string rsvp_cutoff {get; set;}
    //public string fee {get; set;}
    //public string questions {get; set;}
    //public string rsvp_closed {get; set;}
    //public string updated {get; set;}
    public string rsvpcount {get; set;}
    //public string utc_time {get; set;}
    //public string rating {get; set;}
    //public string feedesc {get; set;}
    //public string rating_count {get; set;}
    //public string lat {get; set;}
    public Rating rating { get; set; }
    public List<Host> event_hosts { get; set; }

    public string ReviewCountSummary { get { return rating == null? "" : string.Format("({0} reviews)", rating.Count); } }



    //public string rsvp_open_time {get; set;}
    //public string allow_maybe_rsvp {get; set;}
    //public string rsvp_limit {get; set;}
    //public string utc_rsvp_cutoff {get; set;}
    //public string member_status {get; set;}
    //public string feecurrency {get; set;}
    public string photo_album_id { get; set; }
    public string group_photo_url { get; set; }
    public string ismeetup { get; set; }
    public string payment_required { get; set; }
    public string organizer_name { get; set; }
    public string no_rsvpcount { get; set; }
    public string attendee_count { get; set; }
    public string group_name { get; set; }
    public string lon { get; set; }   

   
    private DateTime internalTime
    {
        get
        {
            if (time == 0) return new DateTime();
            DateTime dt70 = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            long ticks1970 = dt70.Ticks;

            System.DateTime t = new DateTime((time * TimeSpan.TicksPerMillisecond) + ticks1970);
            return t.ToLocalTime();
        }
    }

    public String DateTime
    {
        get
        {
            return internalTime == System.DateTime.MinValue ? "TBD" : internalTime.ToString("f");
        }
    }

    public string Date
    {
        get { return internalTime == System.DateTime.MinValue ? "TBD" : internalTime.ToString("D"); }
    }

    public string Time
    {
        get { return internalTime == System.DateTime.MinValue ? "TBD" : internalTime.ToString("h:mm tt"); }
    }


    public DateTime GetDate()
    {
        return internalTime;
    }
    

    private bool IsCheckinWindow()
    {
        //return true;
        TimeSpan diff = GetDate().Subtract(System.DateTime.Now);
        return (diff.Days <= 0 && diff.Hours <= 1);

    }

    public event CheckinCompleteEventHandler CheckinComplete;
    public event RsvpCompleteEventHandler RsvpComplete;

    protected virtual void OnCheckinComplete(CheckinEventArgs e)
    {
        if (CheckinComplete != null)
            CheckinComplete(this, e);
    }

    protected virtual void OnRsvpComplete(RsvpEventArgs e)
    {
        if (RsvpComplete != null)
            RsvpComplete(this, e);
    }


    public void Rsvp(RSVPType rsvpType)
    {
        Rsvp(rsvpType, "", 0);
    }

    public void Rsvp(RSVPType rsvpType, string comments, int guests)
    {
        var request = new MeetupRequest("rsvp", WebMethod.Post);
        request.AddParameter("event_id", id.ToString());
        request.AddParameter("rsvp", rsvpType.ToString());
        if(comments.Length > 0) request.AddParameter("comments", comments);
        if(guests > 0) request.AddParameter("guests", guests.ToString());


        request.Client.BeginRequest(request, (restRequest, restResponse, userState) =>
        {
            if(restResponse.StatusCode == HttpStatusCode.OK)
            {
                OnRsvpComplete(new RsvpEventArgs("You have RSVP'd " + rsvpType.ToString() + " to this event.", restResponse.Content, true));
            }
            else
            {
                OnRsvpComplete(new RsvpEventArgs("Could not RSVP to this event.", restResponse.Content, false));
            }
        });
        

    }

    public void Checkin()
    {
        if(IsCheckinWindow())
        {
            var request = new MeetupRequest("2/checkin", WebMethod.Post);
            request.AddParameter("event_id", id.ToString());
            request.Client.BeginRequest(request, (restRequest, restResponse, userState) =>
            {
                
                if (restResponse.StatusCode == HttpStatusCode.Created)
                {
                    OnCheckinComplete(new CheckinEventArgs("Checkin successful!", restResponse.Content, true));
                }
                else
                {
                    OnCheckinComplete(new CheckinEventArgs("There was a problem checking in to this event.", restResponse.Content, false));
                }
            });
        }
        else
        {
            OnCheckinComplete(new CheckinEventArgs("You cannot check in to this event at this time."));
        }

    }

   
}

public delegate void CheckinCompleteEventHandler(object sender, CheckinEventArgs e);
public delegate void RsvpCompleteEventHandler(object sender, RsvpEventArgs e);

public class CheckinEventArgs : EventArgs
{
    public bool Success { get; set; }
    public string Message { get; set; }
    public string Data { get; set; }
    public CheckinEventArgs(string message)
    {
        Success = false;
        Message = message;
    }

    public CheckinEventArgs(string message, string data, bool success)
    {
        Data = data;
        Message = message;
        Success = success;
    }
}

public class RsvpEventArgs : EventArgs
{
    public bool Success { get; set; }
    public string Message { get; set; }
    public string Data { get; set; }

    public RsvpEventArgs(string message, string data, bool success)
    {
        Data = data;
        Message = message;
        Success = success;
    }
}

public partial class Host
{
    public string member_name { get; set; }
    public string member_id { get; set; }
}